//
//  PrintController.swift
//  ExpressDriver
//
//  Created by sheng on 2017/11/16.
//  Copyright © 2017年 sheng. All rights reserved.
//

import UIKit
import WebKit

class PrintController: UIViewController {
    
    var webView: WKWebView!
    var url: NSURL!
    var serverUrl:String = "https://www.liqisheng.cn/1.pdf"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.white
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "print"), style: .plain, target: self, action: #selector(printOrder))
        
        
        let conf = WKWebViewConfiguration()
        conf.preferences.javaScriptEnabled = true
        conf.preferences.javaScriptCanOpenWindowsAutomatically = true
        
        //获取状态栏高度
        let statusHeight = UIApplication.shared.statusBarFrame.height
        let navigHeight = self.navigationController!.navigationBar.frame.height
        
        let frame = CGRect(x:0, y:statusHeight + navigHeight, width:UIScreen.main.bounds.width, height:UIScreen.main.bounds.height)
        webView = WKWebView(frame: frame,configuration:conf)
        
        url = NSURL(string: serverUrl)
        let request = URLRequest(url: url! as URL)
        webView.load(request)
        
        //加载页面
        webView.load(request)
        self.view.addSubview(webView)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func printOrder() {
        
        let pic = UIPrintInteractionController.shared
        let printInfo : UIPrintInfo = UIPrintInfo(dictionary: nil)
        
        printInfo.outputType = UIPrintInfoOutputType.general
        printInfo.jobName = url.absoluteString!
        
        pic.printInfo = printInfo
        pic.printFormatter = webView.viewPrintFormatter()
        pic.showsPageRange = false
        
        pic.present(animated: true, completionHandler: nil)
    }
    
    
}

